/**
 * Copyright 2011 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.solarchallenge.client.event;

import com.google.solarchallenge.shared.LoginCategory;
import com.google.solarchallenge.shared.exceptions.InvalidURLParamsException;

import java.util.EnumSet;

/**
 * Event category for Solar Challenge. These events will be used by
 * HistoryHandler to tell GWT App which UI should be shown.
 *
 * This is just a reference and not at all exhaustive.
 *
 * @author Arjun Satyapal
 */
public enum EventCategory {
  HOME("home", null),
  LOGIN_UI("login", null),

  // Tokens related to Applicants.
  APPLICANT_REGISTER("applicant_register", null),
  APPLICANT_HOME("applicant_home", EnumSet.of(LoginCategory.APPLICANT)),
  APPLICANT_AGREEMENT("applicant_agreement", EnumSet.of(
      LoginCategory.APPLICANT, LoginCategory.ADMIN)),
  APPLICANT_REGISTER_DOCUMENTS("applicant_register_documents", EnumSet
      .of(LoginCategory.APPLICANT)),
  CREATE_APPLICATION("create_application", EnumSet.of(LoginCategory.APPLICANT)),
  APPLICANT_NEW_APPLICATION("applicant_new_application", EnumSet
      .of(LoginCategory.APPLICANT)),
  MANAGE_APPLICATIONS("manage_applications", EnumSet
      .of(LoginCategory.APPLICANT)),

  // Tokens related to Approvers.
  APPROVER_HOME("approver_home", EnumSet.of(LoginCategory.APPLICANT)),

  // Tokens related to Admin.
  ADMIN_UI("adminui", EnumSet.of(LoginCategory.ADMIN)),
  EDIT_USER_ACCOUNT_DETAILS("edit_user_account_details", EnumSet
      .of(LoginCategory.ADMIN));

  private String token;
  private EnumSet<LoginCategory> restrictedToLoginCategories;

  private EventCategory(String token,
      EnumSet<LoginCategory> restrictedLoginCategories) {
    this.token = token;
    this.restrictedToLoginCategories = restrictedLoginCategories;
  }

  public String getToken() {
    return token;
  }

  public boolean requiresLogin() {
    if (restrictedToLoginCategories == null) {
      return false;
    }

    return true;
  }

  public boolean isUserAllowed(LoginCategory loginCategory) {
    if (restrictedToLoginCategories == null) {
      // All users are allowed.
      return true;
    }

    if (restrictedToLoginCategories.contains(loginCategory)) {
      return true;
    }

    return false;
  }

  public static EventCategory getHistoryEventCategoryByToken(String token)
      throws InvalidURLParamsException {
    String[] params = token.split("\\?");

    for (EventCategory currEvent : EventCategory.values()) {
      if (currEvent.token.equalsIgnoreCase(params[0])) {
        return currEvent;
      }
    }

    throw new InvalidURLParamsException("Invalid History Token : " + token);
  }
}
